package com.tt.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * lenovo
 * 2022/3/6 - 18:37
 */
@Component
public class AuthorizationInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //支持跨域请求
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,X-Zhongtouxi-Token,X-URL-PATH");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

//        IgnoreAuth annotation;
//        if (handler instanceof HandlerMethod) {
//            annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class);
//        } else {
//            return true;
//        }
//
//        //如果有@IgnoreAuth注解，则不验证token
//        if (annotation != null) {
//            return true;
//        }
//
//        //从header中获取token
//        String token = request.getHeader(LOGIN_TOKEN_KEY);
//        //如果header中不存在token，则从参数中获取token
//        if (StringUtils.isBlank(token)) {
//            token = request.getParameter(LOGIN_TOKEN_KEY);
//        }
//
//        //token为空
//        if (StringUtils.isBlank(token)) {
//            throw new ApiRRException("请先登录", 401);
//        }
//
////        查询token信息
//        TokenEntity tokenEntity = tokenService.queryByToken(token);
//        if (tokenEntity == null) {
//            throw new ApiRRException("无效token，请重新登录", 401);
//        }
//
////        设置userId到request里，后续根据userId，获取用户信息
//        request.setAttribute(LOGIN_USER_KEY, tokenEntity.getConsumerId());
        return true;
    }
}
